const app = getApp();
const typeList = [{
  value: "",
  name: "全部",
}, {
  value: 1,
  name: "支出",
}, {
  value: 2,
  name: "收入",
}];
const pageSize = 20;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    income_money: 0,
    pay_money: 0,
    detailedList: [],
    typeList,
    page: 1,
    contdetail: '',
    ispopshow: false,
    empty: false,
    loadmore: false,
    nomore: false,
    isFilter: false,
    conditions: {
      typeIndex: "",
      start_time: '', //开始时间
      end_time: '', //结束时间
    },
    drawer: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCapitalRecordList();
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },


  reloadData() {
    this.setData({
      page: 1,
      pay_money: 0,
      income_money: 0,
      loadmore: false,
      nomore: false,
      detailedList: [],
    })
    wx.showLoading();
    this.getCapitalRecordList();
  },


  //获取余额记录
  getCapitalRecordList() {
    app.ajax({
      url: 'Partner/Achievement/wapCapitalRecordList',
      data: {
        "page": this.data.page,
        "results": 1,
        "row": pageSize,
        "start_time": this.data.conditions.start_time,
        "end_time": this.data.conditions.end_time,
        "money_type": this.data.conditions.typeIndex !== "" ? this.data.typeList[this.data.conditions.typeIndex].value : "",
      },
      success: (res) => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code > 0) {
          const source = res.data.data;
          const list = this.data.detailedList;
          if (source.data.length > 0 && list.length > 0) {
            if (list[list.length - 1].date == source.data[0].date) {
              let last = list[list.length - 1].list.concat(source.data[0].list);
              list.pop();
              source.data[0].list = last;
            }
          }
          let lists = list.concat(source.data);
          let len = 0;
          lists.map(current => {
            len += current.list.length
          })
          console.log("len",len,"total_number",source.page.total_number );
          this.setData({
            drawer: false,
            detailedList: lists,
            pay_money: source.pay_money,
            income_money: source.income_money,
            loadmore: len < source.page.total_number ? true : false,
            nomore: len >= source.page.total_number ? true : false,
            empty: source.page.page_count == 0 ? true : false,
          })
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误")
      }
    })
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.getCapitalRecordList();
    }
  },




  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },


  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },



  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },


  //查看省略文字
  lookmore(e) {
    var that = this;
    that.setData({
      contdetail: e.currentTarget.dataset.txt,
      ispopshow: true
    });
  },
  //关闭小黑窗户
  closeShow() {
    this.setData({
      ispopshow: false
    })
  },

})